//
//  UIView+SwizzleMethod.m
//  MethodReplaceDemo
//
//  Created by shoguncao on 14-1-3.
//  Copyright (c) 2014年 shoguncao. All rights reserved.
//

#import "UIView+SwizzleMethod.h"
#import <objc/runtime.h>

@implementation UIView (SwizzleMethod)

+ (BOOL)swizzleMethod:(SEL)origSel withMethod:(SEL)altSel
{
    Method originMethod = class_getInstanceMethod(self, origSel);
    Method newMethod = class_getInstanceMethod(self, altSel);
    if (originMethod && newMethod) {
        if (class_addMethod(self, origSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
            class_replaceMethod(self, altSel, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
        } else {
            method_exchangeImplementations(originMethod, newMethod);
        }
    }
    return NO;
}

+ (BOOL)swizzleClassMethod:(SEL)origSel withMethod:(SEL)altSel
{
    Class c = object_getClass(self);
    return [c swizzleMethod:origSel withMethod:altSel];
}

- (void)myDrawRect:(CGRect)rect
{
    [self myDrawRect:rect];
    [[UIColor redColor] set];
    UIRectFill(rect);
}

@end
